//
//  main.m
//  ObjLangDemo
//
//  Created by Tank on 16/2/21.
//  Copyright © 2016年 qtone. All rights reserved.
//

#import <Foundation/Foundation.h>

#pragma mark - 定义类
@interface Person : NSObject

// 定义属性
@property (nonatomic, copy) NSString *name;
@property (nonatomic) int age;

// 定义实例方法
- (id)initWithName:(NSString *)name age:(int)age;
- (NSString *)say;

@end

@implementation Person

- (id)initWithName:(NSString *)name age:(int)age {
    if (self = [super init]) {
        _name = name;
        _age = age;
    }
    return self;
}

- (NSString *)say {
    return [NSString stringWithFormat:@"大家好，我是%@, 我今年%d", self.name, _age];
}

@end

#pragma mark - 继承类
@interface Boss : Person

@property (nonatomic, copy) NSString *company;

@end

@implementation Boss

- (NSString *)say {
    return [NSString stringWithFormat:@"%@, 我创办的公司是:%@", [super say], self.company];
}

@end

#pragma mark - 扩展类
@interface Boss (Ext)

- (NSString *)companyIntro;

@end

@implementation Boss(Ext)

- (NSString *)companyIntro {
    return @"全通教育专注于中国K12基础教育领域的互联网应用和信息服务运营";
}

@end

#pragma mark - 协议与委托
// 老板委托
@protocol BossDelegate <NSObject>

- (void)workInQtone:(NSString *)role;
@optional
- (void)manageCompany:(NSString *)role;

@end

// 声明所有员工
@interface Staff : Person <BossDelegate>

@property (nonatomic, assign) id<BossDelegate> bossDelegate;

- (void)myJob;

@end
@implementation Staff

- (void)myJob {
    if ([self.bossDelegate respondsToSelector:@selector(workInQtone:)]) {
        [self.bossDelegate workInQtone:self.name];
    }
}

@end

// 声明CEO
@interface CEO : Staff
@end

@implementation CEO

- (void)myJob {
    [super myJob];
    if ([self.bossDelegate respondsToSelector:@selector(manageCompany:)]) {
        [self.bossDelegate manageCompany:self.name];
    }
}

@end

// 员工工作
@interface Work : NSObject <BossDelegate>

- (void)staffWork;
- (void)ceoWork;
- (void)staffWork:(void (^)(Staff* staff))myJob;
- (void)ceoWork:(void (^)(CEO *ceo))myJob;

@end

@implementation Work

- (void)staffWork {
    Staff *staff = [[Staff alloc] init];
    staff.name = @"普通员工1";
    staff.bossDelegate = self;
    [staff myJob];
}

- (void)ceoWork {
    CEO *ceo = [[CEO alloc] init];
    ceo.name = @"CEO1";
    ceo.bossDelegate = self;
    [ceo myJob];
}

- (void)staffWork:(void (^)(Staff *))myJob {
    Staff *staff = [[Staff alloc] init];
    staff.name = @"普通员工2";
    myJob(staff);
}

- (void)ceoWork:(void (^)(CEO *))myJob {
    CEO *ceo = [[CEO alloc] init];
    ceo.name = @"CEO2";
    myJob(ceo);
}

#pragma mark - 实现BossDelegate
- (void)workInQtone:(NSString *)role {
    NSLog(@"大家好，我是全通%@", role);
}

- (void)manageCompany:(NSString *)role {
    NSLog(@"我是%@，我还要帮老板管理公司", role);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        // NSLog(@"Hello, World!");
        Person *pony = [[Person alloc] init];
        pony.name = @"Pony";
        pony.age = 45;
        NSLog(@"%@", [pony say]);
        
        Person *jack = [[Person alloc] initWithName:@"Jack" age:52];
        NSLog(@"%@", [jack say]);
        
        Boss *boss = [[Boss alloc] initWithName:@"陈老板" age:45];
        boss.company = @"全通教育";
        NSLog(@"%@", [boss say]);
        NSLog(@"公司介绍：%@",[boss companyIntro]);
        
        Work *work = [Work new];
        [work staffWork];
        [work ceoWork];
        
        [work staffWork:^(Staff *staff) {
            NSLog(@"大家好，我是全通%@", staff.name);
        }];
        [work ceoWork:^(CEO *ceo) {
            NSLog(@"我是%@，我还要帮老板管理公司", ceo.name);
        }];
    }
    return 0;
}
